//
//  QKThreeLinkageView.swift
//  HHSwift
//
//  Created by 华宏 on 2019/10/10.
//  Copyright © 2019 huahong. All rights reserved.
//

import UIKit

protocol QKThreeLinkageViewDelegate {
    func threeLinkageViewClicked(_ cityId:NSNumber?,_ cityName:String?,_ companyId:NSNumber?,_ companyName:String?,_ serviceId:NSNumber?,_ serviceName:String?)
}

class QKThreeLinkageView: UIView {
    
    var currentButton : UIButton!
    

    var cityId:NSNumber? = nil
    var cityName:String? = nil
    var companyId:NSNumber? = nil
    var companyName:String? = nil
    var serviceId:NSNumber? = nil
    var serviceName:String? = nil

    
  fileprivate  var cityModel : CityModel?
  fileprivate  var companyModel : CompanyModel?
  fileprivate  var serviceModel : ServiceModel?
  fileprivate  var cityButton : UIButton!
  fileprivate  var companyButton : UIButton!
  fileprivate  var serviceButton : UIButton!

    typealias QKThreeLinkageViewBlock = (_ cityId:NSNumber?,_ cityName:String?,_ companyId:NSNumber?,_ companyName:String?,_ serviceId:NSNumber?,_ serviceName:String?) -> Void
    var threeLinkageViewBlock : QKThreeLinkageViewBlock?
    var delegate : QKThreeLinkageViewDelegate?
    
    fileprivate  var dataArray : Array<CityModel>!{
        willSet{
            if newValue.count == 1 {
                self.cityModel = newValue.first
                self.cityId = self.cityModel?.ceaId
                self.cityName = self.cityModel?.ceaName
                self.cityButton.setTitle(self.cityName, for: .normal)
                self.setEdgeInsets(button:self.cityButton)
                
                self.cityButton.isUserInteractionEnabled = false
                
                if let companyList = self.cityModel?.areaList,companyList.count == 1 {
                   self.companyModel = companyList.first
                   self.companyId = self.companyModel?.ceaId
                   self.companyName = self.companyModel?.ceaName
                   self.companyButton.setTitle(self.companyName, for: .normal)
                   self.setEdgeInsets(button:self.companyButton)
                   self.companyButton.isUserInteractionEnabled = false

                    
                    if let serviceList = self.companyModel?.areaList,serviceList.count == 1 {
                       self.serviceModel = serviceList.first
                       self.serviceId = self.serviceModel?.ceaId
                       self.serviceName = self.serviceModel?.ceaName
                       self.serviceButton.setTitle(self.serviceName, for: .normal)
                       self.setEdgeInsets(button:self.serviceButton)
                       self.serviceButton.isUserInteractionEnabled = false

                        
                        
                    }
                    
                }
                
            }
            
            
        }
        
        didSet{
           self.delegate?.threeLinkageViewClicked(self.cityId,self.cityName,self.companyId,self.companyName,self.serviceId,self.serviceName)
            self.threeLinkageViewBlock?(self.cityId,self.cityName,self.companyId,self.companyName,self.serviceId,self.serviceName)
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
                
        CCSManager.shared.getCityModelList { (dataArray) in
         
            self.dataArray = dataArray
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        
        let titles = ["城市","分公司","服务中心"]
        let width:CGFloat = self.bounds.size.width / CGFloat(titles.count)
        
        for (index,title) in titles.enumerated(){
            
            let button = UIButton.init(type: .custom)
            button.frame = CGRect(x: width * CGFloat(index), y: 0, width: width, height: self.bounds.size.height)
            button.setTitle(title, for: .normal)
            button.setTitleColor(.gray, for: .normal)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            button.titleLabel?.numberOfLines = 2
            button.setImage(UIImage(named: "arrow"), for: .normal)
            button.layer.borderWidth = 0.5
            button.layer.borderColor = UIColor.lightGray.cgColor
            button.tag = index
            button.addTarget(self, action: #selector(buttonClicked(button:)), for: .touchUpInside)
            self.setEdgeInsets(button:button)

            self.addSubview(button)
            
            switch index {
            case 0:
                self.cityButton = button
            case 1:
            self.companyButton = button
            case 2:
            self.serviceButton = button
            default: break
                
            }
        }
    }
    
    
  @objc  func buttonClicked(button: UIButton) {
    
    var dataSource:[String] = []

    switch button.tag {
        
    case 0:
        
        if self.dataArray == nil {
            return
        }
        
        for cityModel in self.dataArray {
            
            if let ceaName = cityModel.ceaName {
                dataSource.append(ceaName)
            }
        
        }
    case 1:
        
        for companyModel in self.cityModel?.areaList ?? [] {

            if let ceaName = companyModel.ceaName {
                dataSource.append(ceaName)
            }
             
        }
        
    case 2:
        
        for serviceModel in self.companyModel?.areaList ?? [] {
             
            if let ceaName = serviceModel.ceaName {
                       dataSource.append(ceaName)
              }
             
        }
        
    default: break
        
    }
    
    if dataSource.count <= 0 {
           return
       }
    let pickerView = QKPickerView(dataArray: dataSource)
    pickerView.show()
    pickerView.sureBtnBlock = { (selectedRow:Int,selectedText:String) in

    button.setTitle(selectedText, for: .normal)
    self.setEdgeInsets(button:button)


    switch button.tag {
    case 0:
        self.cityModel = self.dataArray[selectedRow]
        self.cityId = self.cityModel?.ceaId
        self.cityName = self.cityModel?.ceaName
        
        self.companyModel = nil
        self.companyId = nil
        self.companyName = nil
        self.companyButton.setTitle("分公司", for: .normal)
        self.setEdgeInsets(button:self.companyButton)

        
        self.serviceModel = nil
        self.serviceId = nil
        self.serviceName = nil
        self.serviceButton.setTitle("服务中心", for: .normal)
        self.setEdgeInsets(button:self.serviceButton)
        
     case 1:
        if let areaList = self.cityModel?.areaList {
           self.companyModel = areaList[selectedRow]
            self.companyId = self.companyModel?.ceaId
            self.companyName = self.companyModel?.ceaName
           
            self.serviceModel = nil
            self.serviceId = nil
            self.serviceName = nil
            self.serviceButton.setTitle("服务中心", for: .normal)
            self.setEdgeInsets(button:self.serviceButton)
        }
       
     case 2:
        
        if let areaList = self.companyModel?.areaList {
            
            self.serviceModel = areaList[selectedRow]
            self.serviceId = self.serviceModel?.ceaId
            self.serviceName = self.serviceModel?.ceaName
        }
                  
        
        
    default:
        break
        
    }
    
    self.delegate?.threeLinkageViewClicked(self.cityId,self.cityName,self.companyId,self.companyName,self.serviceId,self.serviceName)
    self.threeLinkageViewBlock?(self.cityId,self.cityName,self.companyId,self.companyName,self.serviceId,self.serviceName)
   }
    
    
    }
    
   
    func setEdgeInsets(button:UIButton) {
      
        button.layoutIfNeeded()
        
        let titleLabelWidth = (button.titleLabel?.bounds.size.width ?? 0) + 3
        let imageWidth = (button.imageView?.bounds.size.width ?? 0) + 3
               
       button.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: -imageWidth, bottom: 0, right: imageWidth)
       button.imageEdgeInsets = UIEdgeInsets.init(top: 0, left: titleLabelWidth, bottom: 0, right: -titleLabelWidth)
            
        }
    
}
